package com.wwwarehouse.resource_center.fragment.rules;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.android.flexbox.FlexboxLayout;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.rules.AllocationConditionBean;
import com.wwwarehouse.resource_center.bean.rules.OperatorTypeBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/8/10.
 *         <p/>
 *         选择操作者
 */

public class SelectOperatorFragment extends TheParentFragment {

    private static final int GET_OPERATOR_TYPE = 1;
    private static final int GET_ALLOCATION_CONDITION = 2;
    private Button mBtn;
    //    private RadioGroup   operatorType ;
//    private RadioGroup   conditionCode ;
    ArrayList<AllocationConditionBean> conditionBeen;
    ArrayList<OperatorTypeBean> typeBeen;
    private FlexboxLayout mOperatorType;
    private FlexboxLayout mConditionCode;
    private TextView tv_statu;
    private ArrayList<TextView> mTextList;
    private ArrayList<TextView> mConditionCodeList;

    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_select_operator;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof SelectOperatorFragment)
            mActivity.setTitle(getString(R.string.select_operator));
    }

    @Override
    protected void init(View view) {
        mBtn = findView(view, R.id.confirm_btn);
        tv_statu = findView(view, R.id.tv_statu);
//        operatorType = findView(view,R.id.operatorType);
//        conditionCode = findView(view,R.id.conditionCode);
        mOperatorType = findView(view, R.id.operatorType);
        mConditionCode = findView(view, R.id.conditionCode);
    }

    @Override
    protected void loadDatas() {
        mTextList = new ArrayList<>();
        mConditionCodeList = new ArrayList<>();
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isSelectOperator = false;
                if (typeBeen != null) {
                    for (int i = 0; i < typeBeen.size(); i++) {
                        if (typeBeen.get(i).isSelect()) {
                            isSelectOperator = true;
                            break;
                        }
                    }
                }

                boolean isSelectValueCode = false;
                if (conditionBeen != null) {
                    for (int i = 0; i < conditionBeen.size(); i++) {
                        List<AllocationConditionBean.ConditionValuesBean> conditionValues = conditionBeen.get(i).getConditionValues();
                        isSelectValueCode = false;
                        for (int i1 = 0; i1 < conditionValues.size(); i1++) {
                            if (conditionValues.get(i1).isSelect()) {
                                isSelectValueCode = true;
                                break;
                            }
                        }
                    }
                }
                if (!isSelectOperator) {
                    toast("请选择对象");
                    return;
                }
                if (!isSelectValueCode) {
                    toast("请选择状态");
                    return;
                }
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("conditions", conditionBeen);
                objectMap.put("typeBeen", typeBeen);
                EventBus.getDefault().post(new RulesEvent(RulesEvent.RULE_OPERATOR, objectMap));
                popFragment();
            }
        });
        httpPost(ResourceConstant.GET_OPERATOR_TYPE, null, GET_OPERATOR_TYPE, false, "");
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case GET_OPERATOR_TYPE:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    typeBeen = (ArrayList<OperatorTypeBean>) JSON.parseArray(commonClass.getData().toString(), OperatorTypeBean.class);
                    for (int i = 0; i < typeBeen.size(); i++) {
                        final TextView tv = new TextView(mActivity);
                        tv.setBackgroundResource(R.drawable.selector_single_select_button);
                        tv.setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
                        tv.setText(typeBeen.get(i).getName());
                        tv.setGravity(Gravity.CENTER);
                        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.setMargins(0, 0, ConvertUtils.dip2px(mActivity, 15), ConvertUtils.dip2px(mActivity, 15));
                        tv.setLayoutParams(params);
                        final int finalI = i;
                        tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (v.isSelected()) {
                                    return;
                                }
                                for (int i1 = 0; i1 < mTextList.size(); i1++) {
                                    if ((mTextList.get(i1)).isSelected()) {
                                        mTextList.get(i1).setSelected(false);
                                        mTextList.get(i1).setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
                                    }
                                }
                                for (int i1 = 0; i1 < typeBeen.size(); i1++) {
                                    typeBeen.get(i1).setSelect(false);
                                }
                                typeBeen.get(finalI).setSelect(true);
                                tv.setSelected(true);
                                tv.setTextColor(getResources().getColor(R.color.white));
                                //请求接口
                                getAllocationCondition(typeBeen.get(finalI).getCode());
                            }
                        });
                        mOperatorType.addView(tv);
                        mTextList.add(tv);
                    }
                }

                break;
            case GET_ALLOCATION_CONDITION:
                conditionBeen = (ArrayList<AllocationConditionBean>) JSON.parseArray(commonClass.getData().toString(), AllocationConditionBean.class);
                if (conditionBeen != null && conditionBeen.size() > 0) {
                    mConditionCodeList.clear();
                    mConditionCode.removeAllViews();
                    tv_statu.setVisibility(View.VISIBLE);
                    for (int i = 0; i < conditionBeen.size(); i++) {
                        final List<AllocationConditionBean.ConditionValuesBean> conditionValues = conditionBeen.get(i).getConditionValues();
                        for (int i1 = 0; i1 < conditionValues.size(); i1++) {
                            final TextView tv = new TextView(mActivity);
                            tv.setBackgroundResource(R.drawable.selector_single_select_button);
                            tv.setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
                            tv.setText(conditionValues.get(i1).getValueName());
                            tv.setGravity(Gravity.CENTER);
                            FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT);
                            params.setMargins(0, 0, ConvertUtils.dip2px(mActivity, 15), ConvertUtils.dip2px(mActivity, 15));
                            tv.setLayoutParams(params);
                            final int finalI = i1;
                            tv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    for (int i1 = 0; i1 < mConditionCodeList.size(); i1++) {
                                        if ((mConditionCodeList.get(i1)).isSelected()) {
                                            mConditionCodeList.get(i1).setSelected(false);
                                            mConditionCodeList.get(i1).setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
                                        }
                                    }
                                    for (int i1 = 0; i1 < conditionValues.size(); i1++) {
                                        conditionValues.get(i1).setSelect(false);
                                    }
                                    conditionValues.get(finalI).setSelect(true);
                                    tv.setSelected(true);
                                    tv.setTextColor(getResources().getColor(R.color.white));
                                }
                            });
                            mConditionCode.addView(tv);
                            mConditionCodeList.add(tv);
                        }
                    }
                } else {
                    tv_statu.setVisibility(View.GONE);
                    mConditionCode.removeAllViews();
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    private void getAllocationCondition(String code) {
        Map<String, Object> obj = new HashMap<>();
        obj.put("operatorType", code);
        httpPost(ResourceConstant.GET_ALLOCATION_CONDITION, obj, GET_ALLOCATION_CONDITION);
    }
}
